package com.punai.config.sqlitedao;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.punai.common.core.domain.entity.SysPnDictionary;
import com.punai.common.core.text.Convert;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.sqlite.SqliteUtils;
import com.punai.config.constants.PnConfigConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class SqliteDictionaryRepository {
	private final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	@Autowired
	private SqliteUtils sqliteUtils;

	public List<Map<String,String>> findDictionarycodeByPcode(String Pcode) throws ServiceException{
		String sql="SELECT "+
				"dictionary.* "+
				"FROM tab_sys_dictionary dictionary "+
				"INNER JOIN tab_sys_dictionary dictionary1 ON dictionary1.ID = dictionary.PARENT_ID";
		sql+=" where dictionary1.CODE='"+Pcode+"'";
		//initSqliteConnection();
		List<Map<String, Object>> list = sqliteUtils.list(sql);
		List<Map<String,String>>  newlist =new ArrayList<>();
		for (Map<String, Object> map : list) {
			if(MapUtil.isNotEmpty(map)){
				String code=map.get("CODE")==null?null:map.get("CODE").toString();
				String name=map.get("NAME")==null?null:map.get("NAME").toString();
				newlist.add(new HashMap<String,String>(){{
					put("code", code);
					put("name", name);
				}});
			}
		}
		return newlist;
	}
	public List<Map<String,Object>> findOne(String id) throws ServiceException{
		String sql="SELECT "+
				"dictionary.* "+
				"FROM tab_sys_dictionary dictionary ";
		sql+=" where dictionary.id='"+id+"'";
		//initSqliteConnection();
		List<Map<String, Object>> list = sqliteUtils.list(sql);
		return list;
	}

	public Integer getVersion(String id) throws ServiceException{
		String sql="SELECT "+
				"dictionary.VERSION "+
				"FROM tab_sys_dictionary dictionary ";
		sql+=" where dictionary.id='"+id+"'";
		//initSqliteConnection();
		List<Map<String, Object>> list = sqliteUtils.list(sql);
		for (Map<String, Object> stringObjectMap : list) {
			if (stringObjectMap.containsKey("VERSION")) {
				return Convert.toInt(stringObjectMap.get("VERSION"));
			}
		}
		return 0;
	}

	public List<Map<String,Object>> findOneByCode(String code) throws ServiceException{
		String sql="SELECT "+
				"dictionary.* "+
				"FROM tab_sys_dictionary dictionary ";
		sql+=" where dictionary.CODE='"+code+"'";
		//initSqliteConnection();
		List<Map<String, Object>> list = sqliteUtils.list(sql);
		return list;
	}
	public void save(Set<SysPnDictionary> dictionary) throws ServiceException {
		for (SysPnDictionary dictionary2 : dictionary) {
			this.save(dictionary2);
		}
	}
	public void save(SysPnDictionary dictionaryDB) throws ServiceException {
		String sql="";
		int rownumber=0;
		if(findOne(dictionaryDB.getId()).size()>0){
			//update
			sql=" update "
				+ "tab_sys_dictionary "
				+ "set "
				+ "CODE=?,"
				+ "NAME=?,"
				+ "SORTING=?,"
				+ "PARENT_ID=?,"
				+ "PARAMS=?,"
				+ "VALUE=?,"
				+ "REMARK=?,"
				+ "EDIT_STATE=?,"
				+ "LAST_MODIFY=?,"
				+ "SREACH_KEY=?,"
				+ "VERSION=?"
				+ " where ID=? ";
			Object[] args=new Object[] {
					dictionaryDB.getCode(),
					dictionaryDB.getName(),
					dictionaryDB.getSorting(),
					dictionaryDB.getParentId(),
					dictionaryDB.getVarParams(),
					dictionaryDB.getValue(),
					dictionaryDB.getRemarks(),
					PnConfigConstants.EDITSTATE.UPDATESTATE.getType(),
					simpleDateFormat.format(DateUtil.date()),
					dictionaryDB.toString(),
					getVersion(dictionaryDB.getId()) + 1,
					dictionaryDB.getId()
			};
			rownumber= sqliteUtils.execute(sql, args);
		}else{
			//save
			sql=" insert into "
			  	+ "tab_sys_dictionary("
			  	+ "ID,"
				+ "CODE,"
				+ "NAME,"
				+ "SORTING,"
				+ "PARENT_ID,"
				+ "PARAMS,"
				+ "VALUE,"
				+ "REMARK,"
				+ "CREATE_DATE,"
				+ "EDIT_STATE,"
				+ "LAST_MODIFY,"
				+ "SREACH_KEY,"
				+ "VERSION)"
			  	+ " values(?,?,?,?,?,?,?,?,?,?,?,?,?) ";
			 Object[] args=new Object[] {
						dictionaryDB.getId(),
						dictionaryDB.getCode(),
						dictionaryDB.getName(),
						dictionaryDB.getSorting(),
						dictionaryDB.getParentId(),
						dictionaryDB.getVarParams(),
						dictionaryDB.getValue(),
						dictionaryDB.getRemarks(),
						simpleDateFormat.format(dictionaryDB.getCreateTime()),
					 	PnConfigConstants.EDITSTATE.SAVESTATE.getType(),
						simpleDateFormat.format(dictionaryDB.getUpdateTime()),
						dictionaryDB.toString(),
					 	PnConfigConstants.ENTITY_VERSION
			 };
			 rownumber= sqliteUtils.execute(sql, args);
		}
		  if(rownumber==0) {
			  throw new ServiceException("sqlite.message.addException");
		  }
	}
	public void delete(SysPnDictionary dictionaryDB) throws ServiceException {
		String sql="delete from tab_sys_dictionary where id='"+dictionaryDB.getId()+"'";
		//initSqliteConnection();
		int rownumber= sqliteUtils.execute(sql);
		  if(rownumber==0) {
			  throw new ServiceException("sqlite.message.addException");
		  }
	}

	public void deleteAll() throws ServiceException {
		String sql="delete from tab_sys_dictionary";
		//initSqliteConnection();
		int rownumber= sqliteUtils.execute(sql);
		if(rownumber==0) {
			throw new ServiceException("sqlite.message.addException");
		}
	}

	public void beginTransaction() throws ServiceException, SQLException {
		sqliteUtils.begin();
	}

	public void commitTransaction() throws ServiceException, SQLException {
		sqliteUtils.commit();
	}

	public void rollbackTransaction() throws ServiceException, SQLException {
		sqliteUtils.rollback();
	}

}
